|
Every object you see on your screen that can be
located, can be clicked, or moved, is called a window. As you may
imagine, these window objects can be as different as the human eye
can distinguished them. Some of these objects are icons, buttons,
pictures, menu items, etc. As different as they are, there are also
various ways of creating them.
There are three main ways you create a window, as
we will learn throughout this site. One of the techniques you can
use consists of creating a script in a resource file, as we have
introduced scripts in lessons 1 and 2. Another technique you can use
consists of calling one of the Win32 functions such as
CreateWindow() or CreateWindowEx() to create a window;
we have introduced it when creating the main window in all previous
applications. The last option you have is to use your own class and
customize the creation of a window. |
|
In C++, you probably learned that it is a good
idea to make sure that the main() function be not crowded,
which can make your program easier to read and troubleshoot when
problems happen. You can also apply this to Win32 programming by
separating tasks in their own units. Based on this, you can define
functions that perform specific tasks such as creating or
registering the window
To apply object oriented programming (OOP) to a
Win32 application, you can create a class for each window you use in
your application. Since each object is primarily a window, you can
start with a general window that lays a foundation for other
windows. Such a class can be created as follows (this is a primary
window class; we will add methods to it as
needed: | #ifndef WINCTRLS_H
#define WINCTRLS_H
#include <windows.h>
//---------------------------------------------------------------------------
class WControl
{
public:
WControl();
virtual ~WControl();
HWND Create(HINSTANCE hinst, LPCTSTR clsname,
LPCTSTR wndname, HWND parent = NULL,
DWORD dStyle = WS_OVERLAPPEDWINDOW,
int x = CW_USEDEFAULT, int y = CW_USEDEFAULT,
int width = 450, int height = 380);
BOOL Show(int dCmdShow = SW_SHOWNORMAL);
operator HWND();
protected:
HWND hwnd;
HINSTANCE mhInst;
public:
HINSTANCE GetInstance();
private:
};
//---------------------------------------------------------------------------
#endif // WINCTRLS_H
This class can be implemented as follows: #include "winctrl.h"
//---------------------------------------------------------------------------
WControl::WControl()
: hwnd(0)
{
}
//---------------------------------------------------------------------------
WControl::~WControl()
{
}
//---------------------------------------------------------------------------
WControl::operator HWND()
{
return hwnd;
}
//---------------------------------------------------------------------------
HWND WControl::Create(HINSTANCE hinst, LPCTSTR clsname,
LPCTSTR wndname, HWND parent,
DWORD dStyle,
int x, int y,
int width, int height)
{
hwnd = CreateWindow(clsname, wndname, dStyle,
x, y, width, height, parent, NULL, hinst, NULL);
return hwnd;
}
//---------------------------------------------------------------------------
BOOL WControl::Show(int dCmdShow)
{
BOOL CanShow = ::ShowWindow(hwnd, dCmdShow);
if( CanShow )
return TRUE;
return FALSE;
}
//---------------------------------------------------------------------------
HINSTANCE WControl::GetInstance()
{
return mhInst;
}
//---------------------------------------------------------------------------
To use this class, simply declare it in your application
and call the necessary methods. Here is an example: |
|